package com.fingard.dsp.bank.directbank.bocom01;

import com.fingard.constant.Format;
import com.fingard.dsp.bank.directbank.bankInterface.IQryRefund;
import com.fingard.dsp.bank.reqrespobj.qryrefund.ReqQryRefund;
import com.fingard.dsp.bank.reqrespobj.qryrefund.RetAtsQryRefund;
import com.fingard.dsp.bank.reqrespobj.qryrefund.detail.RetAtsRefundDetail;
import com.fingard.xml.XmlTextReader;
import com.fingard.xml.XmlTextWriter;

import java.util.Calendar;

/**
 * 交通银行退票查询
 * 
 * @author Cruise
 * @date 2019/4/22
 */
public class BOCOM01QryRefund extends BOCOM01Base implements IQryRefund {
	@Override
	public RetAtsQryRefund d_qry_refund(ReqQryRefund p_req) throws Exception {
		RetAtsQryRefund retAts = new RetAtsQryRefund(p_req.reqHeader);
        retAts.writeStart();

        String tmpRespCode = "";
        String tmpRespInfo = "";
        
        int tmpSuccCount = 0;
        double tmpTotalAmt = 0;

        try {
        	Calendar tmpCalendarFrom = Calendar.getInstance();
            tmpCalendarFrom.setTime(p_req.getQryStartDate());

            Calendar tmpCalendarTo = Calendar.getInstance();
            tmpCalendarTo.setTime(p_req.getQryEndDate());
            tmpCalendarTo.add(Calendar.DAY_OF_MONTH, 1);
            tr_code  = getBankConfig().getValueAsString("trCode");
            boolean is_new = false;
            if("210240".equals(tr_code)){
                is_new = true;
            }
            while (tmpCalendarFrom.compareTo(tmpCalendarTo) == -1) {
                XmlTextWriter xmlWriter = new XmlTextWriter();
                if(is_new){
                    writeCommonHeader(xmlWriter);
                    xmlWriter.writeStartElement("body");
                    xmlWriter.writeElementString("acno", p_req.payAct);
                    xmlWriter.writeElementString("date", Format.Date8Format.format(tmpCalendarFrom.getTime()));
                }else{
                    tr_code = "310218";
                    writeCommonHeader(xmlWriter);
                    xmlWriter.writeStartElement("body");
                    xmlWriter.writeElementString("accNo", p_req.payAct);
                    xmlWriter.writeElementString("backChequeDate", Format.Date8Format.format(tmpCalendarFrom.getTime()));
                    xmlWriter.writeElementString("currType", "CNY");
                }
                xmlWriter.writeEndDocument();
                String[] tmpRetBkStr = sendToBank(xmlWriter.toXmlString());

                if (tmpRetBkStr[0].length() == 0) {
                    StringBuilder tmpSbMsg = new StringBuilder();
                    XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpRetBkStr[1], tmpSbMsg);

                    if (tmpBkReader != null) {
                    	 String tmpRetCode = tmpBkReader.getFirstText("/ap/head/particular_code");
                         String tmprspmsg = tmpBkReader.getFirstText("/ap/head/particular_info");
                         if (isActiveRetCode(tmpRetCode)) {
                             String serial_record = tmpBkReader.getFirstText("/ap/body/serial_record");
                             int FieldNum = Integer.parseInt(tmpBkReader.getFirstText("/ap/body/field_num"));
                             int RecordNum = Integer.parseInt(tmpBkReader.getFirstText("/ap/body/record_num"));
                             java.util.List<String[]> listStrings = DealMFSText(serial_record, '|', FieldNum, RecordNum);
                             if (listStrings.size() > 0) {
                                 listStrings.remove(0);
                                 if(is_new){
                                     for (String[] strings : listStrings) {
                                     //支付业务流水号（C14）0|会计日期（C8）1|委托日期（C8）2|交易金额(N16.2)3|币种（C3）4|付款人账号（C40）5|付款人名称（C120）6|收款人账号
                                     //（C40）7|收款人名称（C120）8|摘要（C315）9|发起行号（C14）10|接受行号（C14）11|批注说明（C315）12
                                         RetAtsRefundDetail tmpRetDetail = new RetAtsRefundDetail();
                                         tmpRetDetail.askNum = strings[0];
                                         tmpRetDetail.payAct = strings[5];
                                         tmpRetDetail.amount = strings[3];
                                         tmpRetDetail.refundTicketDate = Format.Date10Format.format(tmpCalendarFrom.getTime());
                                         tmpRetDetail.submitTime = Format.DateTimeFormat.format(tmpCalendarFrom.getTime());
                                         tmpRetDetail.refundTicketReason = strings[12];

                                         tmpSuccCount++;
                                         tmpTotalAmt += Double.parseDouble(tmpRetDetail.amount);

                                         retAts.writeRetDetail(tmpRetDetail);
                                     }
                                 }else{
                                     for (String[] strings : listStrings) {
//                                   退票种类0|状态1|我方交换号2|我方账号3|票据种类4|票据号码5|销账号6|退票日期7|退票编号8|退票理由9|金额10|
                                         RetAtsRefundDetail tmpRetDetail = new RetAtsRefundDetail();

                                         tmpRetDetail.askNum = strings[2];
                                         tmpRetDetail.payAct = strings[3];
                                         tmpRetDetail.amount = strings[10];
                                         tmpRetDetail.refundTicketDate = Format.Date10Format.format(tmpCalendarFrom.getTime());
                                         tmpRetDetail.submitTime = Format.DateTimeFormat.format(tmpCalendarFrom.getTime());
                                         tmpRetDetail.refundTicketReason = strings[9];

                                         tmpSuccCount++;
                                         tmpTotalAmt += Double.parseDouble(tmpRetDetail.amount);

                                         retAts.writeRetDetail(tmpRetDetail);
                                     }
                                 }
                             } else {
                                 tmpRespCode = "-1";
                                 tmpRespInfo = tmprspmsg;
                                 break;
                             }
                         } else {
                             if ("2076".equals(tmpRetCode)) {
                                 tmpRespCode = "0";
                             } else {
                                 tmpRespCode = "-1";
                                 tmpRespInfo = tmprspmsg;
                                 break;
                             }
                         }
                    } else {
                        tmpRespCode = "-1";
                        tmpRespInfo = tmpSbMsg.toString();
                    }
                } else {
                    tmpRespCode = "-1";
                    tmpRespInfo = tmpRetBkStr[1];
                }
                
                tmpCalendarFrom.add(Calendar.DAY_OF_MONTH, 1);
            }
            
            retAts.totleAmount = String.valueOf(tmpTotalAmt);
            retAts.totleNum = String.valueOf(tmpSuccCount);
        } catch (Exception ex) {
        	ex.printStackTrace();
            tmpRespCode = "-1";
            tmpRespInfo = ex.getMessage();
            WriteBankLogLn(ex);
        }

        if ("-1".equals(tmpRespCode)) {
            retAts.writeRespCode(tmpRespCode);
            retAts.writeRespInfo(tmpRespInfo);
        } else if (retAts.getWrittenRdCount() == 0) {
            retAts.writeRespCode("0");
            retAts.writeRespInfo("当前时间范围内无退票数据");
        }

        retAts.writeEnd();
        return retAts;
	}
}
